<?php
$PageSecurity = 5;
include_once ('includes/session.inc');

$pageSize = 10;
$action = isset ( $_POST ['action'] ) ? $_POST ['action'] : 'get';

if ($action == 'get') {

    $result = _single ( 'api/partner_prices' )->getAllPartnerPrices ( null, null, null, array () );

    $data = '{"success":true';
    $data .= ',"data":[';
    $haveData = false;

    while ( $row = DB_fetch_array ( $result ) ) {
        $haveData = true;
        $data .= sprintf ( '{"id":"%s","partner_id":"%s","price":"%s","note":"%s"},', $row ['id'], $row ['partner_id'], $row ['price'], $row ['note'] );
    }

    if ($haveData) {
        $data = substr ( $data, 0, strlen ( $data ) - 1 );
    }

    $data .= ']}';

    echo $data;
} elseif ($action == 'getListPartner') {
    $result = _single ( 'api/partner' )->GetAllPartner ( $_SESSION ['UserID'], $_SESSION ['Password'] );

    $data = '{"success":true';
    $data .= ',"data":[';
    $haveData = false;

    while ( $row = DB_fetch_array ( $result ) ) {
        $haveData = true;
        $data .= sprintf ( '{"partner_id":"%s","partner_number":"%s","partner_name":"%s"},', $row ['partner_id'], $row ['partner_number'], $row ['partner_name'] );
    }

    if ($haveData) {
        $data = substr ( $data, 0, strlen ( $data ) - 1 );
    }

    $data .= '],"count":"' . json_encode ( $total ) . '"}';
    echo $data;
} elseif ($action == 'insert') {
    // Get datas:
    $data = array ();
    if (! empty ( $_POST ['partner_id'] ))
        $data ['partner_id'] = DB_escape_string ( trim ( $_POST ['partner_id'] ) );
    if (! empty ( $_POST ['price'] ))
        $data ['price'] = DB_escape_string ( trim ( $_POST ['price'] ) );

    $result = array ();
    $checkNumberExisted = _single ( 'api/partner_prices' )->verifyPartnerPriceExisted ( $data ['partner_id'], $data ['price'] );
    $result ['error'] = 0;
    if ($checkNumberExisted) {
        $result ['error'] = 1;
    } else {
        $result ['id'] = _single ( 'api/partner_prices' )->insert ( $data );
    }
    echo '{"success":true,"data":' . json_encode ( $result ) . '}';
} elseif ($action == 'update') {
    $data = array ();
    if (! empty ( $_POST ['field'] ))
        $data ['field'] = $_POST ['field'];
    if (! empty ( $_POST ['value'] ))
        $data ['value'] = DB_escape_string ( trim ( $_POST ['value'] ) );
    if (! empty ( $_POST ['id'] ))
        $data ['id'] = $_POST ['id'];
        // Execute
    $result = array ();
    $result ['error'] = 0;

    $checkNumberExisted = _single ( 'api/partner_prices' )->verifyPartnerPriceExistedWithField ( $data ['id'], $data ['field'], $data ['value'] );

    if ($checkNumberExisted) {
        $result ['error'] = 1;
    } else {
        $result ['id'] = _single ( 'api/partner_prices' )->update ( $data );
    }
    echo '{"success":true,"data":' . json_encode ( $result ) . '}';
} elseif ($action == 'delete') {
    $data = array ();
    $delete_rs = _single ( 'api/partner_prices' )->delete ( $_POST ['id'] );
    if ($delete_rs === false) {
        echo '{"success":false ,"data":' . json_encode ( null ) . '}';
    } else {
        echo '{"success":true ,"data":' . json_encode ( $_POST ['id'] ) . '}';
    }
}
function getConditions(&$conditions, $field, $relation, $value) {
    if (isset ( $value ) && (! empty ( $value ) || $field == 'locked')) {
        $value = ($relation == 'like') ? "N'%" . DB_escape_string ( trim ( $value ) ) . "%'" : DB_escape_string ( trim ( $value ) );
        array_push ( $conditions, array (
                'field' => $field,
                'relation' => $relation,
                'value' => $value
        ) );
    }
    ;
}
;
?>
